package cn.edu.zafu.bigdata.x.wgj.f;

public class ArrayIndexExceptionHandling {
    public static int getElement(int[] arr,int index){
        if(index<0||index>=arr.length){
            throw new ArrayIndexOutOfBoundsException("索引超出数组范围");
        }
        return arr[index];
    }
    public static void main(String[] args){
        int[] array={1,2,3};
        int invalidIndex=5;
        try{
            int element=getElement(array,invalidIndex);
            System.out.println("指定索引处的元素："+element);
        }catch (ArithmeticException e){
            System.out.println("捕获到数组下标越界异常"+e.getMessage());
        }
    }
}
